﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BlackBBS.Infrastructure.Repository;
using BlackBBS.Models.Data;

namespace BlackBBS.Tests
{
    [TestClass]
    public class TemplateTests
    {
        private string UserFilename = null;

        public TemplateTests()
        {
            this.UserFilename = @"C:\bbs\db\" + typeof(User).ToString().Replace(".", "_") + ".xml";
        }

        [TestMethod]
        public void Test_User_Template_Processing()
        {
            using (var db = new XMLRepository<User>(this.UserFilename))
            {
                var userLogin = "CmdrTallen";
                var user = User.GetUserByLogin(db, userLogin);
                Assert.IsTrue(user != null, "User Fetch Failed");
                Assert.IsTrue(user.Login.Equals(userLogin), "User Fetch Failed");

                var template = "Welcome ${user.firstname}!";    

                var result = user.ProcessTemplate(template);

                Assert.IsTrue(!result.Equals(template), "Template Processing Failed");

                var sb = new StringBuilder();
                foreach (var t in user.TemplateItems)
                {
                    var p = user.ProcessTemplate(t.Key);
                    sb.AppendLine(string.Format("Item [{0}]: Value [{1}]", t.Key, p));
                }
                Assert.IsTrue(sb.ToString() != string.Empty, "Template Processing Failed");                
            }            
        }
    }
}
